<?php

class Product extends AppModel {
	var $name = 'Product';
	var $displayField = 'name';
	var $belongsTo = array('Category', 'Manufacturer');
	var $hasMany = array(
		'Deal' => array('className' => 'Deal', 'foreignKey' => 'product_id', 'dependent' => true),
		'Viewhistory' => array('className' => 'Viewhistory', 'foreignKey' => 'product_id', 'dependent' => true),
		'Buyhistory' => array('className' => 'Buyhistory', 'foreignKey' => 'product_id', 'dependent' => true)
	);

	var $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Name is required.',
				'allowEmpty' => false,
				'required' => true,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		), 
		
		'description' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Description is required.',
				'allowEmpty' => false,
				'required' => true,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'characterlimit'=>array(
				'rule'=>array('checkText'),
				'message'=>'Exceeded character limit. Please shorten your text.',
				'allowEmpty' => true,
				'required' => false,
			
			),
		),
		
		'image_form' => array(
			'corretnType'=>array(
				  'rule' => array('checkImage'),
				  'message' => 'Please use jpeg, png or gif images only.',
					'allowEmpty' => true,
					'required' => false,
			),
			'checksize'=>array(
				'rule'=>array('checkSize'),
				'message'=>'File size is too big.',
				
			),
			/*
			'notempty' => array(
				'rule' => array('checkEmpty'),
				'message' => 'Please upload an image.',
				'allowEmpty' => true,
				'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			*/
		
						
		),

	);
/*
  function noComma() {  
    return !(strpos($this->data['Location']['name'], ',') > -1);
  }
  */
  function checkSize(){
		if (isset($this -> data['Product']['image_form'])) {
			if(empty($this->data['Product']['image'])){ //for edit(has image in db or not)
				if(is_array($this -> data['Product']['image_form']) && $this->data['Product']['image_form']['name']){
					$imgSize = $this -> data['Product']['image_form']['size'];
					if($imgSize>MAX_IMG_SIZE){
						return false;
					}
					else return true;
				}
				else{
					return true;
				}
			}
			else return true;
		}
		else return false; 
  }
 
   function checkImage() {
		if (isset($this -> data['Product']['image_form'])) {
			if(empty($this->data['Product']['image'])){ //for edit(has image in db or not)
				  if (is_array($this -> data['Product']['image_form']) && $this->data['Product']['image_form']['name']) {
					$imgType = $this -> data['Product']['image_form']['type'];

					return in_array($imgType, $this -> data['allowedImgTypes']);

				  } else {
					return true;
				  }
			}else return true;

		} else  return false;
  }
  function checkEmpty(){
		if (isset($this -> data['Product']['image_form'])) {
			if(empty($this->data['Product']['image'])){ //for edit(has image in db or not)
				
				if(is_array($this -> data['Product']['image_form']) && $this->data['Product']['image_form']['name']){
					return true;
				}
				else{
					return false;
				}
			}
			else return true;
		}
		else {

		  return false;
		}  
  }
  
  function checkText(){
		if(isset($this->data['Product']['description'])){
			if(strlen(strip_tags($this->data['Product']['description']))>10000){
				return false;
			}
			else return true;
		}
		else return true;
	}
	
	function ibless_count_update($force_update = false)
	{
		// Removed as no longer needed
	}
}
?>
